package info.ideatower.compass.web;

import com.alibaba.fastjson.JSONObject;
import info.ideatower.compass.entity.Menu;
import info.ideatower.compass.entity.SystemInfo;
import info.ideatower.compass.service.MenuService;
import info.ideatower.compass.service.SystemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

@Controller
public class MainController {

    @Autowired
    MenuService menuService;

    @Autowired
    SystemInfoService sysService;

    @GetMapping("/{sysName}")
    public String getMenus(@PathVariable("sysName") String sysName, Model model) {
        SystemInfo sysInfo = sysService.getSystemInfoByName(sysName);
        if (sysInfo == null) {
            model.addAttribute("errCode", 404);
            model.addAttribute("errMessage", "没有找到该系统信息，请确认路径");
            return "404";
        }
        Map<String, List<Menu>> menuGroup = menuService.getMenuListByName(sysInfo.getId());
        model.addAttribute("menuGroup", menuGroup);
        model.addAttribute("blankGroup", Menu.BLANK_GROUP);
        model.addAttribute("sysInfo", sysInfo);
        return "main";
    }

    @GetMapping("/{sysName}/login")
    public String login(@PathVariable("sysName") String sysName, Model model) {
        SystemInfo sysInfo = sysService.getSystemInfoByName(sysName);
        if (sysInfo == null) {
            model.addAttribute("errCode", 404);
            model.addAttribute("errMessage", "没有找到该系统信息，请确认路径");
            return "404";
        }
        model.addAttribute("sysInfo", sysInfo);
        return "login";
    }
}
